﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GovernmentOfficeProject.LevelBusinessLogic;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.SysTools;
using System.Data;

namespace GovernmentOfficeProject.LevelViewUI.admin
{
    public partial class UserManagerPrivilege : System.Web.UI.Page
    {
        #region 类型定义

        private enum selectMasterType
        {
            role,
            user
        }

        private enum selectButtonType
        {
            condition,
            all
        }
        #endregion

        private static BLL_USER userBLL = null;
        private static BLL_RoleManager roleBLL = null;
        private static List<Entity_USER_User> userList = null;
        private static List<Entity_USER_Role> roleList = null;
        private static string strIsMasterType = selectMasterType.role.ToString();       //默认为用户组操作,与控件ddlMasterType相关.
        private static string strIsBtnSelect = selectButtonType.all.ToString();          //默认为查看所有,与控件btnSelect和btnSelectALL相关,用于重新设置PageIndexChanging事件中的绑定值判断.
        private static decimal selectRoleID = 0;
        private static decimal selectUserID = 0;
        private static string strCurrentMenuNo = "";    //GridViewAccess_RowDataBound用.

        //页面加载:默认为角色(控件显示、GridViewMaster是绑定角色的List)
        //setp-1.0初始化当前页全局默认使用值:
        private bool initGlobalUseValue()
        {
            try
            {
                userBLL = new BLL_USER();
                roleBLL = new BLL_RoleManager();
                userList = userBLL.getAllUser();
                roleList = roleBLL.getRoleList();
                //ddlMasterType.SelectedValue = "PER_ROLE";
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限判断
            if (SystemContext.CurrentUserID == 0 || SystemContext.IsAdmin == 0)
            {
                Response.Redirect("/LevelViewUI/admin/Login.htm?FromUrl=" + HttpUtility.UrlEncode(Request.Url.AbsoluteUri));
            }
            else
            {
                SystemContext.CurrentUserID = SystemContext.CurrentUserID;
                SystemContext.IsAdmin = 1;
            }
            if (!Page.IsPostBack)
            {
                initGlobalUseValue();   //初始化页面全局使用值.
                this.lblSelectValue.Text = "类型名称:";
                this.divRoleGridview.Visible = true;
                this.divUserGridview.Visible = true;
            }
        }

        //显示操作状态的label内容设置:
        private void operationStateShow(string strInfo)
        {
            this.lblOperationMessage.Text = "操作状态:";
            this.lblOperationMessage.Text = this.lblOperationMessage.Text + strInfo;
        }

        //选择需要控制的类型是角色(用户组)还是用户.
        protected void ddlMasterType_SelectedIndexChanged(object sender, EventArgs e)
        {
            operationStateShow("");
            if (this.ddlMasterType.SelectedValue.Equals("PER_ROLE"))    //如果选择角色.
            {
                strIsMasterType = selectMasterType.role.ToString();
                this.lblSelectValue.Text = "用户组名称:";
                this.btnSelectALL.Text = "所有用户组";
                this.divRoleGridview.Visible = true;
                this.divUserGridview.Visible = false;
                this.txtSelectValue.Text = "";
                /*绑定*/
                //this.GridViewMasterRole.DataBind();
                this.bindRoleGridView();
                this.GridViewMasterUser.DataBind();
                this.GridViewAccess.DataBind();
            
            }
            if (this.ddlMasterType.SelectedValue.Equals("PER_USER"))    //如果选择用户.
            {
                strIsMasterType = selectMasterType.user.ToString();
                this.lblSelectValue.Text = "用户登录名:";
                this.btnSelectALL.Text = "所有用户";
                this.divRoleGridview.Visible = false;
                this.divUserGridview.Visible = true;
                this.txtSelectValue.Text = "";
                /*绑定*/
                this.GridViewMasterRole.DataBind();
                //this.GridViewMasterUser.DataBind();
                this.bindUserGridView();
                this.GridViewAccess.DataBind();
            }
            if (this.ddlMasterType.SelectedValue.Equals("PleaseSelect"))
            {
                GridViewAccess.DataBind();
                GridViewMasterRole.DataBind();
                GridViewMasterUser.DataBind();
            }
        }



        /*------------------↓Role的相关页面执行↓---------------------*/

        //[Role]-生成选定行的事件:行创建时加入选定行的触发脚本,脚本内容为调用gridview的SelectedIndexChanged事件.
        protected void GridViewMasterRole_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onclick", "javascirpt:__doPostBack('GridViewMasterRole','Select$" + e.Row.RowIndex + "')");
                e.Row.Attributes["style"] = "Cursor:pointer";
            }
        }

        //[Role]-选定行后执行,与GridViewAccess相关,将选定的RoleID传给相关显示权限的GridViewAccess的方法.
        protected void GridViewMasterRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            operationStateShow("RoleID:"+GridViewMasterRole.SelectedDataKey["RoleID"].ToString());
            strIsMasterType = selectMasterType.role.ToString();
            selectRoleID = Convert.ToDecimal(GridViewMasterRole.SelectedDataKey["RoleID"].ToString());  //设置要显示权限的roleID.
            GridViewAccess.DataSource = getMenuAndButtonDefaultList();  //绑定默认值,在GridViewAccess_RowDataBound事件中设置权限.
            GridViewAccess.DataBind();
            Entity_USER_Role roleShow = new BLL_RoleManager().getRoleByRoleID(Convert.ToDecimal(GridViewMasterRole.SelectedDataKey["RoleID"].ToString().Trim()));
            operationStateShow("已选择用户组[" + roleShow.RoleName + "]");
        }

        //[Role]-为GridViewMasterRole绑定所有用户组.
        private bool bindRoleGridView()
        {
            try
            {
                bool flag = false;
                if (roleList != null)
                {
                    flag = bindRoleGridView(roleList);
                }
                else
                {
                    flag = bindRoleGridView(roleBLL.getRoleList());
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        }

        //[Role]-根据传入list为GridViewMasterRole绑定用户组.
        private bool bindRoleGridView(List<Entity_USER_Role> roleList)
        {
            try
            {
                if (roleList==null)
                {
                    this.GridViewMasterRole.DataSource = null;
                    this.GridViewMasterRole.DataBind();
                }
                else
                {
                    this.GridViewMasterRole.DataSource = roleList;
                    this.GridViewMasterRole.DataBind();
                }
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
        }

        //[Role]-使用自定义数据源,重新设置PageIndexChanging事件为实现GridViewMasterRole分页正常.
        protected void GridViewMasterRole_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (strIsBtnSelect.Equals(selectButtonType.all.ToString()))  //获取所有用户组给GridViewMasterRole绑定数据源,false获取符合查询条件的用户组给GridViewMasterRole绑定数据源.
            {
                GridViewMasterRole.PageIndex = e.NewPageIndex;
                bindRoleGridView();
            }
            else
            {
                Entity_USER_Role role = new Entity_USER_Role();
                role.RoleName = this.txtSelectValue.Text.Trim();
                //AppLog.Write("RoleNmae:" + role.RoleName, AppLog.LogMessageType.Debug);
                if (role.RoleName == null || role.RoleName.Equals("") || roleBLL.getRoleByCondition(role).Count<1)
                {
                    bindRoleGridView(null);
                    operationStateShow("没有找到相关用户组,请检查查找条件.");
                }
                else
                {
                    GridViewMasterRole.PageIndex = e.NewPageIndex;
                    bindRoleGridView(roleBLL.getRoleByCondition(role));
                }
            }
        }




        /*------------------↓User的相关页面执行↓---------------------*/
        //[User]-生成选定行的事件:行创建时加入选定行的触发脚本,脚本内容为调用gridview的SelectedIndexChanged事件.
        protected void GridViewMasterUser_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onclick", "javascirpt:__doPostBack('GridViewMasterUser','Select$" + e.Row.RowIndex + "')");
                e.Row.Attributes["style"] = "Cursor:pointer";
            }
        }

        //[User]-选定行后执行,与GridViewAccess相关,将选定的UserID传给相关显示权限的GridViewAccess的方法.
        protected void GridViewMasterUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            operationStateShow("UserID:" + GridViewMasterUser.SelectedDataKey["UserID"].ToString());
            strIsMasterType = selectMasterType.user.ToString();
            selectUserID = Convert.ToDecimal(GridViewMasterUser.SelectedDataKey["UserID"].ToString());  //设置要显示权限的userID.
            GridViewAccess.DataSource = getMenuAndButtonDefaultList();  //绑定默认值,在GridViewAccess_RowDataBound事件中设置权限.
            GridViewAccess.DataBind();
            Entity_USER_User userShowParameter = new Entity_USER_User();
            userShowParameter.UserID = Convert.ToDecimal(GridViewMasterUser.SelectedDataKey["UserID"].ToString().Trim());
            Entity_USER_User userShow = new BLL_USER().getUserByUser(userShowParameter);
            operationStateShow("已选择用户[" + userShow.LoginName + "]");
        }

        //[User]-为GridViewMasterUser绑定所有用户.
        private bool bindUserGridView()
        {
            try
            {
                bool flag = false;
                if (userList != null)
                {
                    flag = bindUserGridView(userList);
                }
                else
                {
                    flag = bindUserGridView(userBLL.getAllUser());
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        }

        //[User]-根据传入list为GridViewMasterUser绑定用户.
        private bool bindUserGridView(List<Entity_USER_User> userList)
        {
            try
            {
                if (roleList == null)
                {
                    this.GridViewMasterUser.DataSource = null;
                    this.GridViewMasterUser.DataBind();
                }
                else
                {
                    this.GridViewMasterUser.DataSource = userList;
                    this.GridViewMasterUser.DataBind();
                }
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
        }
        
        //[User]-使用自定义数据源时,重新设置PageIndexChanging事件为实现GridViewMasterUser分页正常.
        protected void GridViewMasterUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (strIsBtnSelect.Equals(selectButtonType.all.ToString()))  //获取所有用户给GridViewMasterUser绑定数据源,false获取符合查询条件的用户给GridViewMasterUser绑定数据源.
            {
                GridViewMasterUser.PageIndex = e.NewPageIndex;
                bindUserGridView();
            }
            else
            {
                Entity_USER_User user = new Entity_USER_User();
                user.LoginName = this.txtSelectValue.Text.Trim();
                if (user.LoginName == null || user.LoginName.Equals("") || userBLL.getUserListByCondition(user).Count<1)
                {
                    bindUserGridView(null);
                    operationStateShow("没有找到相关用户,请检查查找条件.");
                }
                else
                {
                    GridViewMasterUser.PageIndex = e.NewPageIndex;
                    bindUserGridView(userBLL.getUserListByCondition(user));
                }
            }
        }



        /*------------------↓Role和User通用的相关页面执行↓---------------------*/
        //查找按钮点击事件:1.验证是查找是RoleList还是UserList,并绑定到GridView.
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            //operationStateShow("");
            GridViewAccess.DataBind();
            if (ddlMasterType.SelectedValue.Equals("PleaseSelect"))
            {
                operationStateShow("请首先选择控制类型,谢谢.");
            }
            else
            {
                strIsBtnSelect = selectButtonType.condition.ToString(); //设置最后点击为条件查找按钮.
                if (strIsMasterType.Equals(selectMasterType.role.ToString()))
                {
                    string roleName = this.txtSelectValue.Text.Trim();
                    if (roleName != "" && roleName != null)
                    {
                        Entity_USER_Role roleEntity = new Entity_USER_Role();
                        roleEntity.RoleName = roleName;
                        List<Entity_USER_Role> roleList = roleBLL.getRoleByCondition(roleEntity);
                        if (roleList.Count >= 1)
                        {
                            bindRoleGridView(roleList);
                            operationStateShow("已获取相关用户组.");
                        }
                        else
                        {
                            operationStateShow("没有相关名称的用户组,请检查查找条件.");
                        }                        
                    }
                    else
                    {
                        bindRoleGridView(null);
                        operationStateShow("没有相关名称的用户组,请检查查找条件.");
                    }
                }
                if (strIsMasterType.Equals(selectMasterType.user.ToString()))
                {
                    //添加User的相关查找操作.
                    string userName = this.txtSelectValue.Text.Trim();
                    if (userName != "" && userName != null)
                    {
                        Entity_USER_User userEntity = new Entity_USER_User();
                        userEntity.LoginName = userName;
                        List<Entity_USER_User> userList = userBLL.getUserListByCondition(userEntity);
                        if (userList.Count >= 1)
                        {
                            bindUserGridView(userList);
                            operationStateShow("已获取相关用户.");
                        }
                        else
                        {
                            operationStateShow("没有相关用户,请检查查找条件.");
                        }
                        
                    }
                    else
                    {
                        bindUserGridView(null);
                        operationStateShow("没有相关用户,请检查查找条件.");
                    }
                }
            }
        }
        //获取全部的按钮点击事件:
        protected void btnSelectALL_Click(object sender, EventArgs e)
        {
            initGlobalUseValue();      //新修改.
            operationStateShow("");
            GridViewAccess.DataBind();
            if (ddlMasterType.SelectedValue.Equals("PleaseSelect"))
            {
                operationStateShow("请首先选择控制类型,谢谢.");
            }
            else
            {
                strIsBtnSelect = selectButtonType.all.ToString();    //设置最后点击为查看所有按钮.
                if (strIsMasterType.Equals(selectMasterType.role.ToString()) || strIsMasterType.Equals(null))
                {
                    bindRoleGridView();
                    operationStateShow("已加载所有用户组信息.");
                }
                if (strIsMasterType.Equals(selectMasterType.user.ToString()))
                {
                    bindUserGridView();
                    operationStateShow("已加载所有用户信息.");
                }
            }
        }

        //这是历时几天想来的.鸡和蛋的问题必须先有鸡,鸡是来自于数据库创造.
        private List<Entity_SYS_MenuAndButtonCollection> getMenuAndButtonDefaultList()
        {
            BLL_SYSTEM systemBLL = new BLL_SYSTEM();
            List<Entity_SYS_MenuAndButtonCollection> mbList = new List<Entity_SYS_MenuAndButtonCollection>();
            List<Entity_SYS_Menu> mList = new BLL_SYSTEM().getAllMenu();
            List<Entity_SYS_Button> bList = new BLL_SYSTEM().getAllButton();
            List<Entity_SYS_Button> bListByMenuNo = null;
            int iOrder = 1;
            foreach (Entity_SYS_Menu menuOne in mList)
            {
                if (menuOne.MenuParentNo == "")
                {
                    Entity_SYS_MenuAndButtonCollection mbEntity = new Entity_SYS_MenuAndButtonCollection();
                    mbEntity.OrderID = iOrder;
                    mbEntity.MenuID = menuOne.MenuID;
                    mbEntity.MenuName = menuOne.MenuName;
                    mbEntity.MenuNo = menuOne.MenuNo;
                    mbEntity.MenuParentNo = menuOne.MenuParentNo;
                    mbList.Add(mbEntity);
                    iOrder++;
                }
                //根据当前menuNo获取所有button
                Entity_SYS_Button btnEntity = new Entity_SYS_Button();
                btnEntity.BtnMenuNo = menuOne.MenuNo;
                bListByMenuNo = systemBLL.getButtonListByCondition(btnEntity);
                //AppLog.Write("bListByMenuNo-Count:" + bListByMenuNo.Count.ToString(), AppLog.LogMessageType.Debug);
                if (!bListByMenuNo.Count.Equals(0))
                {
                    foreach (Entity_SYS_Button btnOne in bListByMenuNo)
                    {
                        Entity_SYS_MenuAndButtonCollection mbEntity = new Entity_SYS_MenuAndButtonCollection();
                        mbEntity.OrderID = iOrder;
                        mbEntity.MenuID = menuOne.MenuID;
                        mbEntity.MenuName = menuOne.MenuName;
                        mbEntity.MenuNo = menuOne.MenuNo;
                        mbEntity.MenuParentNo = menuOne.MenuParentNo;
                        mbEntity.ButtonId = btnOne.BtnID;
                        mbEntity.ButtonName = btnOne.BtnName;
                        mbEntity.ButtonNo = btnOne.BtnNo;
                        mbList.Add(mbEntity);
                        iOrder++;
                    }
                }
            }
            //AppLog.Write("mbList-Count:" + mbList.Count.ToString(), AppLog.LogMessageType.Debug);
            return mbList;
        }



        /*------------------↓GridViewAccess控件↓---------------------*/
        //行绑定数据时候判断授权状态并赋值用于显示.
        protected void GridViewAccess_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (strIsMasterType.Equals(selectMasterType.role.ToString()))
            {
                List<Entity_SYS_Privilege> privilegeList = new BLL_Privilege().getPrivilegeListByRoleID(selectRoleID);
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    /*取当前行的值:*/
                    string strMenuName = DataBinder.Eval(e.Row.DataItem, "menuName").ToString();
                    string strButtonName = DataBinder.Eval(e.Row.DataItem, "buttonName").ToString();
                    decimal dclMenuID = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "menuID"));
                    string strMenuNo = DataBinder.Eval(e.Row.DataItem, "menuNo").ToString();
                    string strMenuParentNo = DataBinder.Eval(e.Row.DataItem, "menuParentNo").ToString();
                    decimal dclButtonID = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "buttonID"));
                    decimal dclOrderID = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "orderID"));
                    if (strButtonName == "" && dclButtonID == 0)  //没有功能按钮值并且button为默认值0(未赋值)的是模块的顶级.
                    {
                        //以后再说的代码编写:判断该模块是否具有权限,若具有所有子模块权限则赋值为授权许可.
                        ((CheckBox)e.Row.FindControl("checkBoxSelect")).Visible = false;
                        //e.Row.Attributes("style", "");
                        e.Row.BackColor = System.Drawing.Color.YellowGreen;
                    }
                    foreach (Entity_SYS_Privilege privilege in privilegeList)
                    {
                        /*获取menu、获取button、判断对应类型，判断是否授权，显示checkbox：*/
                        Entity_SYS_Menu menu = null;
                        Entity_SYS_Button button = null;
                        if (dclMenuID != 0)
                        {
                            menu = new BLL_SYSTEM().getMenuByMenuID(dclMenuID);
                        }
                        if (dclButtonID != 0)
                        {
                            button = new BLL_SYSTEM().getButtonByButtonID(dclButtonID);
                        }
                        //if (strButtonName == "" && dclButtonID == 0)  //没有功能按钮值并且button为默认值0(未赋值)的是模块的顶级.
                        //{
                        //    //以后再说的代码编写:判断该模块是否具有权限,若具有所有子模块权限则赋值为授权许可.
                        //    ((CheckBox)e.Row.FindControl("checkBoxSelect")).Visible = false;
                        //}

                        //Button:btnMenuNo,
                        if (strMenuName != "" && strButtonName != "" && dclButtonID != 0)
                        {
                            if (privilege.PriOperation == "Permit" && privilege.PriAccess == "SYS_Button" && privilege.PriAccessValue == button.BtnID)
                            {
                                ((CheckBox)e.Row.FindControl("checkBoxSelect")).Checked = true;
                            }
                            //↓该语句可以对menu的权限进行选中,但由于menu权限目前仅作为显示用途,而不做实际对应button的权限授权.↓
                            //if (privilege.PriOperation == "Permit" && privilege.PriAccess == "SYS_Menu" && privilege.PriAccessValue == menu.MenuID)
                            //{
                            //    ((CheckBox)e.Row.FindControl("checkBoxSelect")).Checked = true;
                            //}
                        }
                    }
                }
            }
            if (strIsMasterType.Equals(selectMasterType.user.ToString()))
            {
                List<Entity_SYS_Privilege> privilegeList = new BLL_Privilege().getPrivilegeListByUserID(selectUserID);
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    /*取当前行的值:*/
                    string strMenuName = DataBinder.Eval(e.Row.DataItem, "menuName").ToString();
                    string strButtonName = DataBinder.Eval(e.Row.DataItem, "buttonName").ToString();
                    decimal dclMenuID = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "menuID"));
                    string strMenuNo = DataBinder.Eval(e.Row.DataItem, "menuNo").ToString();
                    string strMenuParentNo = DataBinder.Eval(e.Row.DataItem, "menuParentNo").ToString();
                    decimal dclButtonID = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "buttonID"));
                    decimal dclOrderID = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "orderID"));
                    if (strButtonName == "" && dclButtonID == 0)  //没有功能按钮值并且button为默认值0(未赋值)的是模块的顶级.
                    {
                        //以后再说的代码编写:判断该模块是否具有权限,若具有所有子模块权限则赋值为授权许可.
                        ((CheckBox)e.Row.FindControl("checkBoxSelect")).Visible = false;
                        e.Row.BackColor = System.Drawing.Color.YellowGreen;
                    }
                    foreach (Entity_SYS_Privilege privilege in privilegeList)
                    {
                        /*获取menu、获取button、判断对应类型，判断是否授权，显示checkbox：*/
                        Entity_SYS_Menu menu = null;
                        Entity_SYS_Button button = null;
                        if (dclMenuID != 0)
                        {
                            menu = new BLL_SYSTEM().getMenuByMenuID(dclMenuID);
                        }
                        if (dclButtonID != 0)
                        {
                            button = new BLL_SYSTEM().getButtonByButtonID(dclButtonID);
                        }
                        //if (strButtonName == "" && dclButtonID == 0)  //没有功能按钮值并且button为默认值0(未赋值)的是模块的顶级.
                        //{
                        //    //以后再说的代码编写:判断该模块是否具有权限,若具有所有子模块权限则赋值为授权许可.
                        //    ((CheckBox)e.Row.FindControl("checkBoxSelect")).Visible = false;
                        //}

                        //Button:btnMenuNo,
                        if (strMenuName != "" && strButtonName != "" && dclButtonID != 0)
                        {
                            if (privilege.PriOperation == "Permit" && privilege.PriAccess == "SYS_Button" && privilege.PriAccessValue == button.BtnID)
                            {
                                ((CheckBox)e.Row.FindControl("checkBoxSelect")).Checked = true;
                            }
                            //↓该语句可以对menu的权限进行选中,但由于menu权限目前仅作为显示用途,而不做实际对应button的权限授权.↓
                            //if (privilege.PriOperation == "Permit" && privilege.PriAccess == "SYS_Menu" && privilege.PriAccessValue == menu.MenuID)
                            //{
                            //    ((CheckBox)e.Row.FindControl("checkBoxSelect")).Checked = true;
                            //}
                        }
                    }
                }
            }
        }

        //点击checkbox设置授权或取消授权.
        protected void CheckBoxSelect_CheckedChanged(object sender, EventArgs e)
        {
            GridViewRow gdr = (GridViewRow)((CheckBox)sender).Parent.Parent;
            /*取当前行的值:string若为空=="&nbsp"*/
            string strMenuName = "";
            if (gdr.Cells[1].Text.Trim().Equals("&nbsp"))
            { strMenuName = ""; }
            else
            { strMenuName = gdr.Cells[1].Text.Trim(); }
            string strButtonName = "";
            if (gdr.Cells[2].Text.Trim().Equals("&nbsp"))
            { strButtonName = ""; }
            else
            { strButtonName = gdr.Cells[2].Text.Trim(); }
            decimal dclMenuID = Convert.ToDecimal(gdr.Cells[4].Text.Trim());
            string strMenuNo = gdr.Cells[5].Text.Trim();
            string strMenuParentNo = gdr.Cells[6].Text.Trim();
            decimal dclButtonID = Convert.ToDecimal(gdr.Cells[3].Text.Trim());
            decimal dclOrderID = Convert.ToDecimal(gdr.Cells[7].Text.Trim());
            //AppLog.Write("测试当前选中行输出值:-strMenuName=" + strMenuName + "," +
            //    "strButtonName=" + strButtonName + "," +
            //    "dclMenuID=" + dclMenuID.ToString() + "," +
            //    "strMenuNo=" + strMenuNo + "," +
            //    "strMenuParentNo=" + strMenuParentNo + "," +
            //    "dclButtonID=" + dclButtonID.ToString() + "," +
            //    "dclOrderID=" + dclOrderID.ToString() + "-",
            //    AppLog.LogMessageType.Debug);
            if (strIsMasterType.Equals(selectMasterType.role.ToString()))
            {
                if (((CheckBox)sender).Checked)
                {
                    //先取得数据判断是否存在,不存在则在privilege增加授权数据,存在则修改该条数据operation=Permit.
                    //如果是增加,需要再判断是否存在对应menu的数据,如果没有则增加button对应的menu数据在privilege.
                    //如果menu有上级,则继续判断是否存在上级menu的数据,如果没有则增加menu对应的menu上级数据在privilege.
                    //根据byEntity查询private,如果没有则insert操作,如果有则根据privateIDupdate操作.
                    //类型:SYS_Button,值:buttonID,operation=MorF,
                    if (strMenuName != "" && strButtonName != "" && dclButtonID != 0)   //确定当前行是button行.
                    {
                        Entity_SYS_Privilege privilegeEntityParameter = new Entity_SYS_Privilege();
                        privilegeEntityParameter.PriMaster = "PER_Role";
                        privilegeEntityParameter.PriMasterValue = selectRoleID;
                        privilegeEntityParameter.PriAccess = "SYS_Button";
                        privilegeEntityParameter.PriAccessValue = dclButtonID;
                        Entity_SYS_Privilege privilege = new BLL_Privilege().getPrivilegeByCondition(privilegeEntityParameter);
                        //AppLog.Write("privilegeID:" + privilege.PriID.ToString(), AppLog.LogMessageType.Debug);
                        if (privilege.PriID <= 0)    //根据条件没取得数据,说明private表没有该条button数据,需要新增.否则根据privateID修改这条数据.
                        {
                            /*新增需要判断对应的menu是否存在private表,如果menu有上级则由上级到下级menu在到具体button的多条insert.*/
                            bool flag = false;
                            Entity_SYS_Privilege priMenuParmeter = new Entity_SYS_Privilege();
                            priMenuParmeter.PriMaster = "PER_Role";
                            priMenuParmeter.PriMasterValue = selectRoleID;
                            priMenuParmeter.PriAccess = "SYS_Menu";
                            priMenuParmeter.PriAccessValue = dclMenuID;
                            Entity_SYS_Privilege priMenu = new BLL_Privilege().getPrivilegeByCondition(priMenuParmeter);
                            //AppLog.Write("priMenuID:" + priMenu.PriID.ToString(), AppLog.LogMessageType.Debug);
                            //if (priMenu.PriID <= 0)    //如果没有menu的privilege记录,则新增,新增的operation=permit.
                            //{
                                //判断有没上级,没有则添加menu的privilege数据.有上级则先取得上级并判断上级是否存在privilege,不存在则新增上级.
                                Entity_SYS_Menu menu = new BLL_SYSTEM().getMenuByMenuID(dclMenuID);
                                if (menu.MenuParentNo != "")    //说明还有上级,先更新或增加上级.(该系统目前设计为两级menu)
                                {
                                    Entity_SYS_Menu menuParameter = new Entity_SYS_Menu();
                                    menuParameter.MenuNo = menu.MenuParentNo;
                                    //AppLog.Write("menuParameter.MenuNoValue:" + menuParameter.MenuNo, AppLog.LogMessageType.Debug);
                                    List<Entity_SYS_Menu> menuParentList = new BLL_SYSTEM().getMenuListByCondition(menuParameter);
                                    //AppLog.Write(menuParentList.Count.ToString(), AppLog.LogMessageType.Debug);
                                    Entity_SYS_Menu menuReturn = null;
                                    foreach (Entity_SYS_Menu muByList in menuParentList)
                                    {
                                        menuReturn = new Entity_SYS_Menu();
                                        menuReturn.MenuID = muByList.MenuID;
                                        menuReturn.MenuNo = muByList.MenuNo;
                                        menuReturn.MenuParentNo = muByList.MenuParentNo;
                                    }
                                    //判断menuParent是否在privilege表存在,不存在则新增:
                                    Entity_SYS_Privilege priMenuParentParmeter = new Entity_SYS_Privilege();
                                    priMenuParentParmeter.PriMaster = "PER_Role";
                                    priMenuParentParmeter.PriMasterValue = selectRoleID;
                                    priMenuParentParmeter.PriAccess = "SYS_Menu";
                                    priMenuParentParmeter.PriAccessValue = menuReturn.MenuID;
                                    Entity_SYS_Privilege priMenuParentReturn = new BLL_Privilege().getPrivilegeByCondition(priMenuParentParmeter);
                                    if (priMenuParentReturn.PriID > 0)    //menuParent是否在privilege表存在,判断menuParent的权限是Forbid则更新为Permit.
                                    {
                                        if (priMenuParentReturn.PriOperation == "Forbid")
                                        {
                                            Entity_SYS_Privilege priMenuParentSave = new Entity_SYS_Privilege();
                                            priMenuParentSave.PriOperation = "Permit";
                                            flag = new BLL_Privilege().updatePrivilege(priMenuParentReturn.PriID, priMenuParentSave);
                                        }
                                    }
                                    else //menuParent在privilege表不存在,添加menuParent到privilege.
                                    {
                                        Entity_SYS_Privilege privilegeSave = new Entity_SYS_Privilege();
                                        privilegeSave.PriMaster = "PER_Role";
                                        privilegeSave.PriMasterValue = selectRoleID;
                                        privilegeSave.PriAccess = "SYS_Menu";
                                        privilegeSave.PriAccessValue = menuReturn.MenuID;
                                        privilegeSave.PriOperation = "Permit";
                                        flag = new BLL_Privilege().addPrivilege(privilegeSave);    //保存上级MENU.
                                    }
                                }

                                //判断menu在privilege是否存在,存在则更新为Permit,否则新增menu在privilege,
                                Entity_SYS_Privilege priMenuParmeterVal = new Entity_SYS_Privilege();
                                priMenuParmeterVal.PriMaster = "PER_Role";
                                priMenuParmeterVal.PriMasterValue = selectRoleID;
                                priMenuParmeterVal.PriAccess = "SYS_Menu";
                                priMenuParmeterVal.PriAccessValue = dclMenuID;
                                Entity_SYS_Privilege priMenuReturn = new BLL_Privilege().getPrivilegeByCondition(priMenuParmeterVal);
                                if (priMenuReturn.PriID > 0)    //menuParent是否在privilege表存在,判断menu的权限是Forbid则更新为Permit.
                                {
                                    //AppLog.Write("689-priMenuReturn.PriOperation=" + priMenuReturn.PriOperation, AppLog.LogMessageType.Debug);
                                    if (priMenuReturn.PriOperation == "Forbid")
                                    {
                                        //AppLog.Write("691",AppLog.LogMessageType.Debug);
                                        Entity_SYS_Privilege priMenuParentSave = new Entity_SYS_Privilege();
                                        priMenuParentSave.PriOperation = "Permit";
                                        flag = new BLL_Privilege().updatePrivilege(priMenuReturn.PriID, priMenuParentSave);
                                    }
                                }
                                else //menu在privilege表不存在,添加menu到privilege.
                                {
                                    Entity_SYS_Privilege privilegeSave = new Entity_SYS_Privilege();
                                    privilegeSave.PriMaster = "PER_Role";
                                    privilegeSave.PriMasterValue = selectRoleID;
                                    privilegeSave.PriAccess = "SYS_Menu";
                                    privilegeSave.PriAccessValue = dclMenuID;
                                    privilegeSave.PriOperation = "Permit";
                                    flag = new BLL_Privilege().addPrivilege(privilegeSave);    //保存当前MENU.
                                }
                            //}

                            //增加button在privilege的数据:先判断button在privilege表是否有数据,如果有就更新,没有则新增.
                            Entity_SYS_Privilege priButtonParameter = new Entity_SYS_Privilege();
                            priButtonParameter.PriMaster = "PER_Role";
                            priButtonParameter.PriMasterValue = selectRoleID;
                            priButtonParameter.PriAccess = "SYS_Button";
                            priButtonParameter.PriAccessValue = dclButtonID;
                            Entity_SYS_Privilege priButtonReturn = new BLL_Privilege().getPrivilegeByCondition(priButtonParameter);
                            if (priButtonReturn.PriID > 0)   //button在privilege表有数据,判断是Forbid则更新为Permit.
                            {
                                if (priButtonReturn.PriOperation == "Forbid")
                                {
                                    Entity_SYS_Privilege priButtonParameterValue = new Entity_SYS_Privilege();
                                    priButtonParameterValue.PriOperation = "Permit";
                                    flag = new BLL_Privilege().updatePrivilege(priButtonReturn.PriID, priButtonParameterValue);
                                }
                            }
                            else //button在privilege表没有数据,新增当前button数据到privilege表.
                            {
                                Entity_SYS_Privilege priButtonSave = new Entity_SYS_Privilege();
                                priButtonSave.PriMaster = "PER_Role";
                                priButtonSave.PriMasterValue = selectRoleID;
                                priButtonSave.PriAccess = "SYS_Button";
                                priButtonSave.PriAccessValue = dclButtonID;
                                priButtonSave.PriOperation = "Permit";
                                flag = new BLL_Privilege().addPrivilege(priButtonSave);
                                if (flag)
                                {
                                    operationStateShow("已授权[" + new BLL_RoleManager().getRoleByRoleID(selectRoleID).RoleName + "]用户组-[" + strMenuName + "]-[" + strButtonName + "]的功能.");
                                }
                            }
                        }
                        else
                        {
                            //不为空则更新这条private数据为Primit.
                            Entity_SYS_Privilege privilegeEntityPmtA = new Entity_SYS_Privilege();
                            privilegeEntityPmtA.PriOperation = "Permit";
                            bool flag = new BLL_Privilege().updatePrivilege(privilege.PriID, privilegeEntityPmtA);
                            if (flag)
                            {
                                //判断menu在privilege是否存在,存在则更新为Permit,否则新增menu在privilege,
                                Entity_SYS_Privilege priMenuParmeterVal = new Entity_SYS_Privilege();
                                priMenuParmeterVal.PriMaster = "PER_Role";
                                priMenuParmeterVal.PriMasterValue = selectRoleID;
                                priMenuParmeterVal.PriAccess = "SYS_Menu";
                                priMenuParmeterVal.PriAccessValue = dclMenuID;                                
                                Entity_SYS_Privilege priMenuReturn = new BLL_Privilege().getPrivilegeByCondition(priMenuParmeterVal);
                                if (priMenuReturn.PriID > 0)    //menuParent是否在privilege表存在,判断menu的权限是Forbid则更新为Permit.
                                {
                                    if (priMenuReturn.PriOperation == "Forbid")
                                    {
                                        //AppLog.Write("758", AppLog.LogMessageType.Debug);
                                        Entity_SYS_Privilege priMenuParentSave = new Entity_SYS_Privilege();
                                        priMenuParentSave.PriOperation = "Permit";
                                        flag = new BLL_Privilege().updatePrivilege(priMenuReturn.PriID, priMenuParentSave);
                                    }
                                }
                                operationStateShow("已授权[" + new BLL_RoleManager().getRoleByRoleID(selectRoleID).RoleName + "]用户组-[" + strMenuName + "]-[" + strButtonName + "]的功能.");
                            }
                        }
                    }
                }
                else //取消授权.
                {
                    //如果关系表引用有该role 不能删除
                    bool flag = false;
                    bool blButtonPrivilegeForbid = false;
                    //取消授权,说明privilege已有该条数据,修改operation=Forbid.
                    //取消后判断同级button是否全部取消授权,如果全部取消则更新privilege的button对应menu的operation=Forbid.
                    //如果menu存在上级,则同时修改上级menu在privilege的operation=forbid.                   
                    //先取得该button,再取button对应的menu,再取menu对应的menuParent
                    //获得button
                    Entity_SYS_Privilege privilegeButtonParameter = new Entity_SYS_Privilege();
                    privilegeButtonParameter.PriMaster = "PER_Role";
                    privilegeButtonParameter.PriMasterValue = selectRoleID;
                    privilegeButtonParameter.PriAccess = "SYS_Button";
                    privilegeButtonParameter.PriAccessValue = dclButtonID;
                    Entity_SYS_Privilege privilegeButton = new BLL_Privilege().getPrivilegeByCondition(privilegeButtonParameter);
                    //更新buttonPrivilege
                    //Entity_SYS_Privilege priButtonEntity = new Entity_SYS_Privilege();
                    //priButtonEntity.PriOperation = "Forbid";
                    //blButtonPrivilegeForbid = new BLL_Privilege().updatePrivilege(privilegeButton.PriID, priButtonEntity);
                    //删除buttonPrivilege:     
                    List<Entity_USER_Relation> relationList = new BLL_UserRelation().getRelationListALL();
                    bool blExist = false;
                    foreach (Entity_USER_Relation relation in relationList)
                    {
                        if (relation.UrControlledPrivilegeID==privilegeButton.PriID)
                        {
                            if (blExist==false)
                            {
                                blExist = true;
                                break;
                            }
                        }
                    }
                    if (blExist)
                    {
                        ((CheckBox)sender).Checked = true;
                        operationStateShow("取消授权失败-当前用户组权限已被[用户关系]使用.");
                    }
                    else
                    {
                        blButtonPrivilegeForbid = new BLL_Privilege().deletePrivilege(privilegeButton.PriID);

                        //获得menuPrivilege
                        Entity_SYS_Privilege priMenuParmeter = new Entity_SYS_Privilege();
                        priMenuParmeter.PriMaster = "PER_Role";
                        priMenuParmeter.PriMasterValue = selectRoleID;
                        priMenuParmeter.PriAccess = "SYS_Menu";
                        priMenuParmeter.PriAccessValue = dclMenuID;
                        Entity_SYS_Privilege priMenu = new BLL_Privilege().getPrivilegeByCondition(priMenuParmeter);
                        //判断menu的下属button,如果都没有存在privilege表,则更新priMenu在privilege为Forbid.
                        //取得menu对应的button列表集合
                        Entity_SYS_Button buttonParmeter = new Entity_SYS_Button();
                        buttonParmeter.BtnMenuNo = strMenuNo;   //注意一下!!!
                        List<Entity_SYS_Button> buttonListByMenu = new BLL_SYSTEM().getButtonListByCondition(buttonParmeter);
                        //AppLog.Write("buttonListByMenu-Count:" + buttonListByMenu.Count.ToString(), AppLog.LogMessageType.Debug);
                        //根据获取的button判断在privilege表中是否有数据以及数据的状态是否有Permit的,如果没有则设置menuPrivilege=Forbid.
                        bool blButtonPermitExist = false;
                        foreach (Entity_SYS_Button button in buttonListByMenu)
                        {
                            Entity_SYS_Privilege priButtonParameter = new Entity_SYS_Privilege();
                            priButtonParameter.PriMaster = "PER_Role";
                            priButtonParameter.PriMasterValue = selectRoleID;
                            priButtonParameter.PriAccess = "SYS_Button";
                            priButtonParameter.PriAccessValue = button.BtnID;
                            Entity_SYS_Privilege priReturnByButton = new BLL_Privilege().getPrivilegeByCondition(priButtonParameter);
                            if (priReturnByButton.PriOperation == "Permit")
                            {
                                blButtonPermitExist = true;
                                break;
                            }
                            else
                            {
                                blButtonPermitExist = false;
                            }
                        }
                        if (!blButtonPermitExist)   //如果没有menu下属button或下属button没有为Permit的,则更新menu在privilege的数据为Forbid.
                        {
                            Entity_SYS_Menu menu = new BLL_SYSTEM().getMenuByMenuID(dclMenuID);
                            if (menu.MenuParentNo != "") //如果menu不是顶级菜单则更新,因为顶级菜单在前台始终要显示.
                            {
                                Entity_SYS_Privilege priMenuEntity = new Entity_SYS_Privilege();
                                priMenuEntity.PriOperation = "Forbid";
                                flag = new BLL_Privilege().updatePrivilege(priMenu.PriID, priMenuEntity);
                            }

                            #region 省略上级Menu的授权状态修改.
                            ////因为系统设计为两级模块菜单,所以顶级可以一直显示,没有权限的用户和组只能看到顶级模块菜单而看不到其下属的子模块及功能按钮.所以以下步骤不做处理.
                            ////判断menu是否有上级,如果有上级则根据priMenu==Forbid则上级menu在privilege为Forbid.
                            //Entity_SYS_Menu menu = new BLL_SYSTEM().getMenuByMenuID(dclMenuID);
                            //if (menu.MenuParentNo != "")    //说明还有上级(该系统目前设计为两级menu)
                            //{
                            //    Entity_SYS_Menu menuParameter = new Entity_SYS_Menu();
                            //    menuParameter.MenuNo = menu.MenuParentNo;
                            //    //AppLog.Write("menuParameter.MenuNoValue:" + menuParameter.MenuNo, AppLog.LogMessageType.Debug);
                            //    List<Entity_SYS_Menu> menuParentList = new BLL_SYSTEM().getMenuListByCondition(menuParameter);
                            //}
                            #endregion
                        }
                        if (blButtonPrivilegeForbid)
                        {
                            operationStateShow("已禁止[" + new BLL_RoleManager().getRoleByRoleID(selectRoleID).RoleName + "]用户组-[" + strMenuName + "]-[" + strButtonName + "]的功能.");
                        }
                    }
                }
            }
            /****************************************↑ROLE↑***************************↓USER↓*************************************************/
            if (strIsMasterType.Equals(selectMasterType.user.ToString()))
            {
                if (((CheckBox)sender).Checked)
                {
                    //先取得数据判断是否存在,不存在则在privilege增加授权数据,存在则修改该条数据operation=Permit.
                    //如果是增加,需要再判断是否存在对应menu的数据,如果没有则增加button对应的menu数据在privilege.
                    //如果menu有上级,则继续判断是否存在上级menu的数据,如果没有则增加menu对应的menu上级数据在privilege.
                    //根据byEntity查询private,如果没有则insert操作,如果有则根据privateIDupdate操作.
                    //类型:SYS_Button,值:buttonID,operation=MorF,
                    if (strMenuName != "" && strButtonName != "" && dclButtonID != 0)   //确定当前行是button行.
                    {
                        Entity_SYS_Privilege privilegeEntityParameter = new Entity_SYS_Privilege();
                        privilegeEntityParameter.PriMaster = "PER_User";
                        privilegeEntityParameter.PriMasterValue = selectUserID;
                        privilegeEntityParameter.PriAccess = "SYS_Button";
                        privilegeEntityParameter.PriAccessValue = dclButtonID;
                        Entity_SYS_Privilege privilege = new BLL_Privilege().getPrivilegeByCondition(privilegeEntityParameter);
                        //AppLog.Write("privilegeID:" + privilege.PriID.ToString(), AppLog.LogMessageType.Debug);
                        if (privilege.PriID <= 0)    //根据条件没取得数据,说明private表没有该条button数据,需要新增.否则根据privateID修改这条数据.
                        {
                            /*新增需要判断对应的menu是否存在private表,如果menu有上级则由上级到下级menu在到具体button的多条insert.*/
                            bool flag = false;
                            Entity_SYS_Privilege priMenuParmeter = new Entity_SYS_Privilege();
                            priMenuParmeter.PriMaster = "PER_User";
                            priMenuParmeter.PriMasterValue = selectUserID;
                            priMenuParmeter.PriAccess = "SYS_Menu";
                            priMenuParmeter.PriAccessValue = dclMenuID;
                            Entity_SYS_Privilege priMenu = new BLL_Privilege().getPrivilegeByCondition(priMenuParmeter);
                            //AppLog.Write("priMenuID:" + priMenu.PriID.ToString(), AppLog.LogMessageType.Debug);
                            //if (priMenu.PriID <= 0)    //如果没有menu的privilege记录,则新增,新增的operation=permit.
                            //{
                                //判断有没上级,没有则添加menu的privilege数据.有上级则先取得上级并判断上级是否存在privilege,不存在则新增上级.
                                Entity_SYS_Menu menu = new BLL_SYSTEM().getMenuByMenuID(dclMenuID);
                                if (menu.MenuParentNo != "")    //说明还有上级,先更新或增加上级.(该系统目前设计为两级menu)
                                {
                                    Entity_SYS_Menu menuParameter = new Entity_SYS_Menu();
                                    menuParameter.MenuNo = menu.MenuParentNo;
                                    //AppLog.Write("menuParameter.MenuNoValue:" + menuParameter.MenuNo, AppLog.LogMessageType.Debug);
                                    List<Entity_SYS_Menu> menuParentList = new BLL_SYSTEM().getMenuListByCondition(menuParameter);
                                    //AppLog.Write(menuParentList.Count.ToString(), AppLog.LogMessageType.Debug);
                                    Entity_SYS_Menu menuReturn = null;
                                    foreach (Entity_SYS_Menu muByList in menuParentList)
                                    {
                                        menuReturn = new Entity_SYS_Menu();
                                        menuReturn.MenuID = muByList.MenuID;
                                        menuReturn.MenuNo = muByList.MenuNo;
                                        menuReturn.MenuParentNo = muByList.MenuParentNo;
                                    }
                                    //判断menuParent是否在privilege表存在,不存在则新增:
                                    Entity_SYS_Privilege priMenuParentParmeter = new Entity_SYS_Privilege();
                                    priMenuParentParmeter.PriMaster = "PER_User";
                                    priMenuParentParmeter.PriMasterValue = selectUserID;
                                    priMenuParentParmeter.PriAccess = "SYS_Menu";
                                    priMenuParentParmeter.PriAccessValue = menuReturn.MenuID;
                                    Entity_SYS_Privilege priMenuParentReturn = new BLL_Privilege().getPrivilegeByCondition(priMenuParentParmeter);
                                    if (priMenuParentReturn.PriID > 0)    //menuParent是否在privilege表存在,判断menuParent的权限是Forbid则更新为Permit.
                                    {
                                        if (priMenuParentReturn.PriOperation == "Forbid")
                                        {
                                            Entity_SYS_Privilege priMenuParentSave = new Entity_SYS_Privilege();
                                            priMenuParentSave.PriOperation = "Permit";
                                            flag = new BLL_Privilege().updatePrivilege(priMenuParentReturn.PriID, priMenuParentSave);
                                        }
                                    }
                                    else //menuParent在privilege表不存在,添加menuParent到privilege.
                                    {
                                        Entity_SYS_Privilege privilegeSave = new Entity_SYS_Privilege();
                                        privilegeSave.PriMaster = "PER_User";
                                        privilegeSave.PriMasterValue = selectUserID;
                                        privilegeSave.PriAccess = "SYS_Menu";
                                        privilegeSave.PriAccessValue = menuReturn.MenuID;
                                        privilegeSave.PriOperation = "Permit";
                                        flag = new BLL_Privilege().addPrivilege(privilegeSave);    //保存上级MENU.
                                    }
                                }
                                //判断menu在privilege是否存在,存在则更新为Permit,否则新增menu在privilege,
                                Entity_SYS_Privilege priMenuParmeterVal = new Entity_SYS_Privilege();
                                priMenuParmeterVal.PriMaster = "PER_User";
                                priMenuParmeterVal.PriMasterValue = selectUserID;
                                priMenuParmeterVal.PriAccess = "SYS_Menu";
                                priMenuParmeterVal.PriAccessValue = dclMenuID;
                                Entity_SYS_Privilege priMenuReturn = new BLL_Privilege().getPrivilegeByCondition(priMenuParmeterVal);
                                if (priMenuReturn.PriID > 0)    //menuParent是否在privilege表存在,判断menu的权限是Forbid则更新为Permit.
                                {
                                    if (priMenuReturn.PriOperation == "Forbid")
                                    {
                                        Entity_SYS_Privilege priMenuParentSave = new Entity_SYS_Privilege();
                                        priMenuParentSave.PriOperation = "Permit";
                                        flag = new BLL_Privilege().updatePrivilege(priMenuReturn.PriID, priMenuParentSave);
                                    }
                                }
                                else //menu在privilege表不存在,添加menu到privilege.
                                {
                                    Entity_SYS_Privilege privilegeSave = new Entity_SYS_Privilege();
                                    privilegeSave.PriMaster = "PER_User";
                                    privilegeSave.PriMasterValue = selectUserID;
                                    privilegeSave.PriAccess = "SYS_Menu";
                                    privilegeSave.PriAccessValue = dclMenuID;
                                    privilegeSave.PriOperation = "Permit";
                                    flag = new BLL_Privilege().addPrivilege(privilegeSave);    //保存当前MENU.
                                }
                            //}

                            //增加button在privilege的数据:先判断button在privilege表是否有数据,如果有就更新,没有则新增.
                            Entity_SYS_Privilege priButtonParameter = new Entity_SYS_Privilege();
                            priButtonParameter.PriMaster = "PER_User";
                            priButtonParameter.PriMasterValue = selectUserID;
                            priButtonParameter.PriAccess = "SYS_Button";
                            priButtonParameter.PriAccessValue = dclButtonID;
                            Entity_SYS_Privilege priButtonReturn = new BLL_Privilege().getPrivilegeByCondition(priButtonParameter);
                            if (priButtonReturn.PriID > 0)   //button在privilege表有数据,判断是Forbid则更新为Permit.
                            {
                                if (priButtonReturn.PriOperation == "Forbid")
                                {
                                    Entity_SYS_Privilege priButtonParameterValue = new Entity_SYS_Privilege();
                                    priButtonParameterValue.PriOperation = "Permit";
                                    flag = new BLL_Privilege().updatePrivilege(priButtonReturn.PriID, priButtonParameterValue);
                                }
                            }
                            else //button在privilege表没有数据,新增当前button数据到privilege表.
                            {
                                Entity_SYS_Privilege priButtonSave = new Entity_SYS_Privilege();
                                priButtonSave.PriMaster = "PER_User";
                                priButtonSave.PriMasterValue = selectUserID;
                                priButtonSave.PriAccess = "SYS_Button";
                                priButtonSave.PriAccessValue = dclButtonID;
                                priButtonSave.PriOperation = "Permit";
                                flag = new BLL_Privilege().addPrivilege(priButtonSave);
                                if (flag)
                                {
                                    Entity_USER_User user = new Entity_USER_User();
                                    user.UserID = selectUserID;
                                    operationStateShow("已授权[" + new BLL_USER().getUserByUser(user).LoginName + "]用户-[" + strMenuName + "]-[" + strButtonName + "]的功能.");
                                }
                            }
                        }
                        else
                        {
                            //不为空则更新这条private数据为Primit.
                            Entity_SYS_Privilege privilegeEntityPmtA = new Entity_SYS_Privilege();
                            privilegeEntityPmtA.PriOperation = "Permit";
                            bool flag = new BLL_Privilege().updatePrivilege(privilege.PriID, privilegeEntityPmtA);
                            if (flag)
                            {
                                //判断menu在privilege是否存在,存在则更新为Permit,否则新增menu在privilege,
                                Entity_SYS_Privilege priMenuParmeterVal = new Entity_SYS_Privilege();
                                priMenuParmeterVal.PriMaster = "PER_User";
                                priMenuParmeterVal.PriMasterValue = selectUserID;
                                priMenuParmeterVal.PriAccess = "SYS_Menu";
                                priMenuParmeterVal.PriAccessValue = dclMenuID;
                                Entity_SYS_Privilege priMenuReturn = new BLL_Privilege().getPrivilegeByCondition(priMenuParmeterVal);
                                if (priMenuReturn.PriID > 0)    //menuParent是否在privilege表存在,判断menu的权限是Forbid则更新为Permit.
                                {
                                    if (priMenuReturn.PriOperation == "Forbid")
                                    {
                                        Entity_SYS_Privilege priMenuParentSave = new Entity_SYS_Privilege();
                                        priMenuParentSave.PriOperation = "Permit";
                                        flag = new BLL_Privilege().updatePrivilege(priMenuReturn.PriID, priMenuParentSave);
                                    }
                                }
                                Entity_USER_User user = new Entity_USER_User();
                                user.UserID = selectUserID;
                                operationStateShow("已授权[" + new BLL_USER().getUserByUser(user).LoginName + "]用户-[" + strMenuName + "]-[" + strButtonName + "]的功能.");
                            }
                        }
                    }
                }
                else //取消授权.
                {
                    bool flag = false;
                    bool blButtonPrivilegeForbid = false;
                    //取消授权,说明privilege已有该条数据,修改operation=Forbid.
                    //取消后判断同级button是否全部取消授权,如果全部取消则更新privilege的button对应menu的operation=Forbid.
                    //如果menu存在上级,则同时修改上级menu在privilege的operation=forbid.                   
                    //先取得该button,再取button对应的menu,再取menu对应的menuParent
                    //获得button
                    Entity_SYS_Privilege privilegeButtonParameter = new Entity_SYS_Privilege();
                    privilegeButtonParameter.PriMaster = "PER_User";
                    privilegeButtonParameter.PriMasterValue = selectUserID;
                    privilegeButtonParameter.PriAccess = "SYS_Button";
                    privilegeButtonParameter.PriAccessValue = dclButtonID;
                    Entity_SYS_Privilege privilegeButton = new BLL_Privilege().getPrivilegeByCondition(privilegeButtonParameter);
                    //更新buttonPrivilege
                    //Entity_SYS_Privilege priButtonEntity = new Entity_SYS_Privilege();
                    //priButtonEntity.PriOperation = "Forbid";
                    //blButtonPrivilegeForbid = new BLL_Privilege().updatePrivilege(privilegeButton.PriID, priButtonEntity);
                    //删除buttonPrivilege
                     List<Entity_USER_Relation> relationList = new BLL_UserRelation().getRelationListALL();
                    bool blExist = false;
                    foreach (Entity_USER_Relation relation in relationList)
                    {
                        if (relation.UrControlledPrivilegeID==privilegeButton.PriID)
                        {
                            if (blExist==false)
                            {
                                blExist = true;
                                break;
                            }
                        }
                    }
                    if (blExist)
                    {
                        ((CheckBox)sender).Checked = true;
                        operationStateShow("取消授权失败-该用户权限已被[用户关系]使用.");
                    }
                    else
                    {
                        blButtonPrivilegeForbid = new BLL_Privilege().deletePrivilege(privilegeButton.PriID);
                        //获得menuPrivilege
                        Entity_SYS_Privilege priMenuParmeter = new Entity_SYS_Privilege();
                        priMenuParmeter.PriMaster = "PER_User";
                        priMenuParmeter.PriMasterValue = selectUserID;
                        priMenuParmeter.PriAccess = "SYS_Menu";
                        priMenuParmeter.PriAccessValue = dclMenuID;
                        Entity_SYS_Privilege priMenu = new BLL_Privilege().getPrivilegeByCondition(priMenuParmeter);
                        //判断menu的下属button,如果都没有存在privilege表,则更新priMenu在privilege为Forbid.
                        //取得menu对应的button列表集合
                        Entity_SYS_Button buttonParmeter = new Entity_SYS_Button();
                        buttonParmeter.BtnMenuNo = strMenuNo;   //注意一下!!!
                        List<Entity_SYS_Button> buttonListByMenu = new BLL_SYSTEM().getButtonListByCondition(buttonParmeter);
                        //AppLog.Write("buttonListByMenu-Count:" + buttonListByMenu.Count.ToString(), AppLog.LogMessageType.Debug);
                        //根据获取的button判断在privilege表中是否有数据以及数据的状态是否有Permit的,如果没有则设置menuPrivilege=Forbid.
                        bool blButtonPermitExist = false;
                        foreach (Entity_SYS_Button button in buttonListByMenu)
                        {
                            Entity_SYS_Privilege priButtonParameter = new Entity_SYS_Privilege();
                            priButtonParameter.PriMaster = "PER_User";
                            priButtonParameter.PriMasterValue = selectUserID;
                            priButtonParameter.PriAccess = "SYS_Button";
                            priButtonParameter.PriAccessValue = button.BtnID;
                            Entity_SYS_Privilege priReturnByButton = new BLL_Privilege().getPrivilegeByCondition(priButtonParameter);
                            if (priReturnByButton.PriOperation == "Permit")
                            {
                                blButtonPermitExist = true;
                                break;
                            }
                            else
                            {
                                blButtonPermitExist = false;
                            }
                        }
                        if (!blButtonPermitExist)   //如果没有menu下属button或下属button没有为Permit的,则更新menu在privilege的数据为Forbid.
                        {
                            Entity_SYS_Menu menu = new BLL_SYSTEM().getMenuByMenuID(dclMenuID);
                            if (menu.MenuParentNo != "") //如果menu不是顶级菜单则更新,因为顶级菜单在前台始终要显示.
                            {
                                Entity_SYS_Privilege priMenuEntity = new Entity_SYS_Privilege();
                                priMenuEntity.PriOperation = "Forbid";
                                flag = new BLL_Privilege().updatePrivilege(priMenu.PriID, priMenuEntity);
                            }

                            #region 省略上级Menu的授权状态修改.
                            ////因为系统设计为两级模块菜单,所以顶级可以一直显示,没有权限的用户和组只能看到顶级模块菜单而看不到其下属的子模块及功能按钮.所以以下步骤不做处理.
                            ////判断menu是否有上级,如果有上级则根据priMenu==Forbid则上级menu在privilege为Forbid.
                            //Entity_SYS_Menu menu = new BLL_SYSTEM().getMenuByMenuID(dclMenuID);
                            //if (menu.MenuParentNo != "")    //说明还有上级(该系统目前设计为两级menu)
                            //{
                            //    Entity_SYS_Menu menuParameter = new Entity_SYS_Menu();
                            //    menuParameter.MenuNo = menu.MenuParentNo;
                            //    //AppLog.Write("menuParameter.MenuNoValue:" + menuParameter.MenuNo, AppLog.LogMessageType.Debug);
                            //    List<Entity_SYS_Menu> menuParentList = new BLL_SYSTEM().getMenuListByCondition(menuParameter);
                            //}
                            #endregion
                        }
                        if (blButtonPrivilegeForbid)
                        {
                            Entity_USER_User user = new Entity_USER_User();
                            user.UserID = selectUserID;
                            operationStateShow("已禁止[" + new BLL_USER().getUserByUser(user).LoginName + "]用户-[" + strMenuName + "]-[" + strButtonName + "]的功能.");
                        }
                    }
                }
            }
        }
    }
}

